package com.mojian.controller.user;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mojian.common.Result;
import com.mojian.entity.SysArticle;
import com.mojian.entity.SysUser;
import com.mojian.service.UserService;
import com.mojian.vo.article.ArticleListVo;
import com.mojian.vo.comment.CommentListVo;
import com.mojian.vo.user.UserStatsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

/**
 * @author: quequnlong
 * @date: 2025/1/11
 * @description:
 */
@RestController
@RequestMapping("/protal/user")
@RequiredArgsConstructor
@Api(tags = "门户-个人中心")
@Slf4j
public class UserController {

    private final UserService userService;

    @PutMapping("/updateProfile")
    @ApiOperation(value = "修改我的资料")
    public Result<Void> updateProfile(@RequestBody SysUser user){
        userService.updateProfile(user);
        return Result.success();
    }

    @GetMapping("/comment")
    @ApiOperation(value = "获取我的评论")
    public Result<IPage<CommentListVo>> selectMyComment(){
        return Result.success(userService.selectMyComment());
    }

    @DeleteMapping("/delMyComment/{ids}")
    @ApiOperation(value = "删除我的评论")
    public Result<Void> delMyComment(@PathVariable List<Long> ids){
        return Result.success(userService.delMyComment(ids));
    }

    @GetMapping("/myReply")
    @Operation(description = "获取我的回复")
    public Result<IPage<CommentListVo>> getMyReply() {
        return Result.success(userService.getMyReply());
    }

    @GetMapping("/myLike")
    @ApiOperation(value = "获取我的点赞")
    public Result<IPage<ArticleListVo>> selectMyLike(){
        return Result.success(userService.selectMyLike());
    }

    @GetMapping("/myArticle")
    @ApiOperation(value = "获取我的文章")
    public Result<IPage<ArticleListVo>> selectMyArticle(SysArticle article){
        return Result.success(userService.selectMyArticle(article));
    }
    
    @GetMapping("/statistics")
    @ApiOperation(value = "获取用户统计信息")
    public Result<Map<String, Integer>> getUserStatistics() {
        // 获取文章数量和获赞数量
        int postCount = userService.countUserArticles();
        int likesCount = userService.countUserArticleLikes();
        
        Map<String, Integer> stats = new HashMap<>();
        stats.put("posts", postCount);
        stats.put("likes", likesCount);
        stats.put("followers", 1); // 假设关注者为0，后续可扩展
        
        return Result.success(stats);
    }
    
    @GetMapping("/membership")
    @ApiOperation(value = "获取用户会员信息")
    public Result<Map<String, Object>> getUserMembership() {
        try {
            // 调用会员服务获取当前用户的会员信息
            // 如果有MemberService在这个模块，可以直接注入使用
            // 这里使用简单的实现方式
            Map<String, Object> memberInfo = userService.getUserMembershipInfo();
            if (memberInfo != null) {
                return Result.success(memberInfo);
            } else {
                return Result.success(null); // 用户不是会员
            }
        } catch (Exception e) {
            return Result.error("获取会员信息失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/membershipOrders")
    @ApiOperation(value = "获取用户会员订单列表")
    public Result<Map<String, Object>> getUserMembershipOrders(@RequestParam(defaultValue = "1") Integer pageNum,
                                                             @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            // 获取当前用户ID
            Long userId = StpUtil.getLoginIdAsLong();
            log.info("获取用户 [{}] 的会员订单列表", userId);
            
            Map<String, Object> result = userService.getUserMembershipOrders(pageNum, pageSize);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取会员订单异常", e);
            return Result.error("获取会员订单失败: " + e.getMessage());
        }
    }
}
